﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace CoreLib.MySQL.Mapping
{
    /// <summary>
    /// Class containing all the database information required to link with
    /// the classes
    /// </summary>
    public class DbMap
    {
        //dictionary class v/s columns
        private static Dictionary<Type, List<DbFieldAttribute>> dbMap = new Dictionary<Type, List<DbFieldAttribute>>();

        /// <summary>
        /// Gets all the columns from a table linked to the class
        /// </summary>
        /// <param name="type">Class</param>
        /// <returns></returns>
        public static List<DbFieldAttribute> GetMap(Type type)
        {
            return dbMap[type];
        }

        /// <summary>
        /// Gets all the information from the assembly needed for link the classes
        /// with the database
        /// </summary>
        public static void MapDatabase()
        {
            foreach (Type table in Assembly.GetEntryAssembly().GetTypes())
            {
                foreach(var tableAttr in table.GetCustomAttributes(true))
                {
                    if ((tableAttr as DbTableAttribute) != null)
                    {
                        List<DbFieldAttribute> typeMap = new List<DbFieldAttribute>();
                        foreach (FieldInfo field in table.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
                        {
                            foreach (var attribute in field.GetCustomAttributes(typeof(DbFieldAttribute), true))
                            {
                                DbFieldAttribute column = attribute as DbFieldAttribute;
                                if (column != null)
                                {
                                    column.isProperty = true;
                                    column.memberInClass = field;
                                    typeMap.Add(column);
                                }
                            }
                        }
                        foreach (var field in table.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
                        {
                            foreach (var attribute in field.GetCustomAttributes(typeof(DbFieldAttribute), true))
                            {
                                DbFieldAttribute column = attribute as DbFieldAttribute;
                                if (column != null)
                                {
                                    column.memberInClass = field;
                                    column.isProperty = false;
                                    typeMap.Add(column);
                                }
                            }
                        }

                        if (typeMap.Count > 0)
                        {
                            dbMap.Add(table, typeMap);
                        }
                    }
                }
            }
        }
    }
}
